package com.liqiye.paymanager.controller;

import com.liqiye.paymanager.bean.common.ConvertedResponse;
import com.liqiye.paymanager.service.payment.CurrencyConverterService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 货币接口
 */
@RestController
@RequestMapping("/currency")
public class CurrencyController {

    private final CurrencyConverterService currencyConverterService;

    public CurrencyController(CurrencyConverterService currencyConverterService) {
        this.currencyConverterService = currencyConverterService;
    }

    /**
     * 货币转换
     * @param from 货币代码，如 USD
     * @param to 货币代码，如 CNY
     * @param amount 金额
     */
    @GetMapping("/convert")
    public ResponseEntity<ConvertedResponse> convert(
                                @RequestParam String from,
                                @RequestParam String to,
                                @RequestParam BigDecimal amount) {

        ConvertedResponse convert = currencyConverterService.convert(from.toUpperCase(), to.toUpperCase(), amount);
        return ResponseEntity.ok(convert);
    }
}
